package cn.ac.siom.query.action;

import cn.ac.siom.query.service.ITab_thesis_HotService;
import cn.ac.siom.query.service.IaccessService;
import cn.ac.siom.query.util.action.DefaultAction;
import cn.ac.siom.query.vo.Accessinfo;
import cn.ac.siom.query.vo.Tab_Thesis;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@SuppressWarnings("unchecked")
@Controller
@RequestMapping(value = "/home/*")
public class Itab_thesis_hotAction extends DefaultAction {

    @Resource
    private ITab_thesis_HotService tab_thesis_HotService ;

    @Resource
    private IaccessService iaccessService;

    @ResponseBody
    @RequestMapping(value="find_Tab_hot", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public List<Tab_Thesis> find_list(HttpServletRequest request) throws SQLException {
        insertAccessinfo(request,"首页热点排行");
        return this.tab_thesis_HotService.list();
    }

    @ResponseBody
    @RequestMapping(value="findNO_Tab_hot", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public List<Tab_Thesis> findNO(Integer id,HttpServletRequest request,Accessinfo accessinfo) throws SQLException {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        //String url0[]= request.getHeader("Referer").split("/");
        //String url=url0[1];
        String url =request.getHeader("Referer");
        accessinfo.setIp(ip);
        if(url==null){
            accessinfo.setUrl("/");
        }else{
            accessinfo.setUrl(url);
        }
        accessinfo.setName("热门文献详情页");
        this.iaccessService.insertOne(accessinfo);
        List<Tab_Thesis>  tab_thesis_Hot = this.tab_thesis_HotService.list_NO(id);
        this.tab_thesis_HotService.update(id);
        return tab_thesis_Hot;
    }


    @ResponseBody
    @RequestMapping(value="findPage_Tab_hot", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public Map<String, Object> findPage(Integer page_id,HttpServletRequest request,Accessinfo accessinfo) throws SQLException {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        accessinfo.setIp(ip);
        String url =request.getHeader("Referer");
        if(url==null){
            accessinfo.setUrl("/list.html");
        }else{
            accessinfo.setUrl(url);
        }
        accessinfo.setName("热门文献列表显示");
        this.iaccessService.insertOne(accessinfo);
        System.out.println(page_id);
        List<Tab_Thesis>  tab_thesis_Hots = this.tab_thesis_HotService.list_page(page_id);
        Map<String,Object> map = new HashMap<>();
        map.put("tab_thesis_Hots",tab_thesis_Hots);
        map.put("page_id",page_id);
        return map;
    }

    @ResponseBody
    @RequestMapping(value="findCount_Tab_hot", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public int findCount() throws SQLException {
        Integer cont= this.tab_thesis_HotService.list_count();
        return cont;
    }


    @Override
    public String getText() {
        return null;
    }

}